/*
 * Created on May 1, 2006
 *
 * Copyright (c) 2005 Peter Johan Salomonsen (http://www.petersalomonsen.com)
 * 
 * http://www.frinika.com
 * 
 * This file is part of Frinika.
 * 
 * Frinika is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * Frinika is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Frinika; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package com.petersalomonsen.mediacast.client;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JLabel;

import com.lightminds.appletservice.AppletServiceClientProxy;
import com.petersalomonsen.mediacast.session.CastSessionRemote;

/**
 * @author Peter Johan Salomonsen
 */
public class StreamForward extends Thread {

    JLabel statusLabel = new JLabel();
    
    boolean running = false;
    
    public static int BUFFER_SIZE = 65536;
    
    public void run()
    {
        String fileName="/mediacast/test.stream";
        running= true;
        
        try {    
            statusLabel.setText("Connecting");
            URL url = new URL("http://localhost:1234/");
            
            URLConnection uc = url.openConnection();
                    
            InputStream stream = uc.getInputStream();
            
            // Initialize a session bean object
            
            statusLabel.setText("Resolving stream header");
            CastSessionRemote remoteObj  = (CastSessionRemote)AppletServiceClientProxy.newInstance(CastSessionRemote.class);
            byte[] header = FindHeader.getHeader(url);
            
            statusLabel.setText("Sending stream header");
            remoteObj.sendHeader(fileName,header);
        
            statusLabel.setText("Skipping header for new data");
            while(stream.available() < header.length)
                Thread.yield();
        
            stream.read(header);
            
            int n=1;
            
            while(true)
            {
                byte[] b = new byte[BUFFER_SIZE];
                
                statusLabel.setText("Streaming data packet "+n++);
                
                int byteCount=0;
                while(byteCount < BUFFER_SIZE)
                    byteCount+=stream.read(b,byteCount,BUFFER_SIZE-byteCount);
                
                // Call method on the session bean
                
                remoteObj.sendFragment(fileName,b);
            }
        } catch (Exception e) {
            
            e.printStackTrace();
            running = false;
            statusLabel.setText("Lost connection to stream. Stopped");
        }

    }
    
    public boolean isRunning()
    {
    	return running;
    }
    
    public JLabel getStatus()
    {
        return statusLabel;
    }
}
